package com.dao.impl;

import com.dao.Login;
import com.util.dbCRUD;
import com.entity.Clients;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import static com.util.Vessel.scanner;

public class LoginImpl implements Login {
    //注册
    @Override
    public List<Clients> signUp(String cName,String cPassword) {
        String sql = "insert into clients(cName,cPassword) values(?,?)";
        Object[] objects = new Object[2];
        objects[0] = cName;
        objects[1] = cPassword;

        int num = dbCRUD.update(sql,objects);
        if (num > 0){
            System.out.println("注册成功！");
        }
        String sql1 = "select *\n" +
                "from clients\n" +
                "WHERE cPassword=?" +
                "order by cId DESC\n" +
                "LIMIT 1";
        Object[] object = new Object[1];
        object[0] = cPassword;
        ResultSet resultSet = dbCRUD.printAll(sql1,object);
        List<Clients> clientsList = new ArrayList<>();
        try {
            while(resultSet.next()){
                Clients client = new Clients();
                client.setcId(resultSet.getInt("cId"));
                client.setcName(resultSet.getString("cName"));
                client.setcPassword(resultSet.getString("cPassword"));
                client.setcVip(false);
                client.setcBalance(0);
                clientsList.add(client);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return clientsList;
    }

    //登录
    @Override
    public List<Clients> signIn(int cId,String cPassword) {
        int id = 0;
        String sql = "select cId\n" +
                "from clients\n" +
                "where cId = ? " +
                "and cPassword=?";
        Object[] objects = new Object[2];
        objects[0]=cId;
        objects[1]=cPassword;

        ResultSet resultSet = dbCRUD.printAll(sql,objects);
        List<Clients> clientsList = new ArrayList<>();
        try{
            while(resultSet.next()){
                Clients client = new Clients();
                client.setcId(resultSet.getInt("cId"));
                clientsList.add(client);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return clientsList;
    }
}
